<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;

class XhProjectHistory extends BaseModel
{
    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHProject_History';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsblGroup[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhsblGroup
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 审核
     *
     * @param $idArr
     * @param $state
     * @return bool
     *
     * create by sqb 20190117
     */
    public static function checkProject($idArr, $state)
    {
        $idArr = implode(',', $idArr);
        $res = self::find([
            'conditions' => "iId in ({$idArr})",
        ]);

        if ($res->update(['iState' => $state])) {
            return true;
        }

        return false;
    }

    /**
     * 判断是否为添加的记录
     *
     * @param $idArr
     * @return bool
     *
     * create by sqb 2019017
     */
    public static function isLocalData($idArr)
    {
        foreach ($idArr as $id) {
            $project = self::findFirst($id);
            if ($project->iPlanId) {
                return false;
            }
        }

        return true;
    }

    /**
     * 写入流程计划数据
     *
     * @param $data
     * @return bool
     *
     * create by sqb 20190118
     */
    public static function insterData($data)
    {
        $planId = $data['iPlanId'];//流程计划id
        $cooperate = $data['sCooperate'];//合作方式
        $class = $data['sClass'];//班级 逗号分割的字符串
        $teacher = $data['iTeacher'];//教师数
        $student = $data['iStudent'];//学生数
        $state = $data['iState'];//流程计划状态   1.已完成  2.进行中
        $schoolId = $data['iSchoolId'];//学校id
        $tTime = $data['tTime'];//项目时间 精确到天  如2019-01-18
        $obj = XhProjectHistory::findFirst([
            'conditions' => "bDelete = 0 AND iPlanId = $planId"
        ]);
        if (empty($obj)) {
            $obj = new self();
        }
        $obj->iPlanId = $planId;
        $obj->sCooperate = $cooperate;
        $obj->sClass = $class;
        $obj->iTeacher = $teacher;
        $obj->iStudent = $student;
        $obj->iState = $state;
        $obj->iSchoolId = $schoolId;
        $obj->tTime = $tTime;

        if ($obj->save()) {
            return true;
        }

        return false;
    }

    /**
     * 删除流程计划
     *
     * @param $id
     * @return mixed
     *
     * create by sqb 20190118
     */
    public static function delData($id)
    {
        return self::find([
            'conditions' => 'iPlanId = :id:',
            'bind' => ['id' => $id]
        ])->delete();
    }
}
